//index.js
const app = getApp()

Page({
  data: {
    videos: [],
    videoIndex: 0,
    duration: 500,
    userHidden: false
  },
  onLoad: function () {

    const videos = this.genVideo(7);
    setTimeout(() => {
      this.setData({
        videos
      });
    }, 1000);

    this.onGetOpenid()
  },
  onGetOpenid: function () {
    // 调用云函数
    wx.cloud.callFunction({
      name: 'mylogin',
      data: {},
      success: res => {
        // console.log('[云函数] [login] user openid: ', res.result.openid)
        app.globalData.openid = res.result.openid
        this.setData({
          openid: app.globalData.openid
        })
        this.onQuery()

      },
      fail: err => {
        console.error('[云函数] [login] 调用失败', err)
      },
      complete: () => {
        // this.isNewUser()
      }
    })
  },
  onQuery: function () {
    const db = wx.cloud.database()
    // 查询当前用户所有的 counters
    db.collection('ybjc').where({
      useropenid: this.data.openid
      // _id:"e984b69a5ee6f12c001912e452e8d776"
    }).get({
      success: res => {

        // let userData=JSON.stringify(res.data, null, 2)//？？？
        //!!!
        let userDatas = res.data
        // console.log(userDatas)

        if (userDatas.length > 0) {
          app.globalData.isNewUser = false
          app.globalData.user_id = userDatas[0]._id
          app.globalData.userdates = userDatas[0].userdates
          this.setData({
            userHidden: true
          })
        } else {
          app.globalData.isNewUser = true
          // this.setData({
          //   userHidden:false
          // })
        }
        // console.log(app)
      },
      fail: err => {
        console.error('[数据库] [查询记录] 失败：', err)
      }
    })
  },
  onChange(e) {
    // if(!this.data.myHidePanel){
    //   this.setData({
    //     myHidePanel: true
    //   })
    // }
    // console.log('change', e.detail.video);
  },
  onPlay(e) {
    // console.log('play', e.detail.video);
  },

  onRePlay(e) {
    // console.log('replay', e.detail.video);
  },
  onPause(e) {
    // console.log('myPause', e.detail.video);
  },
  onPanelForward(e) {},
  genVideo(count) {
    const length = this.data.videos.length;
    const videos = [];
    for (let i = 0; i < count; i++) {
      let src = '';
      let poster = '';

      switch (i) {
        case 0:
          src = "";
          poster = 'https://7265-release-mm1hb-1301926727.tcb.qcloud.la/jimu/init.png?sign=c2f471aa9aa2f6354aee391f80ee2c48&t=1592150903';
          break;
        case 1:
          src = "https://7265-release-mm1hb-1301926727.tcb.qcloud.la/yanbaocao/index01.mp4?sign=5e604a98bb82e357e2fcab60fef96ab9&t=1599020727";
          poster = '';
          break;
        case 2:
          src = "https://7265-release-mm1hb-1301926727.tcb.qcloud.la/yanbaocao/index02.mp4?sign=0bd9268f069dbc866560cb7bf14ed688&t=1599020778";
          poster = '';
          break;
        case 3:
          src = "https://7265-release-mm1hb-1301926727.tcb.qcloud.la/yanbaocao/index03.mp4?sign=b94d968c6216f3150db28f701b421063&t=1599020796";
          poster = '';
          break;
        case 4:
          src = "https://7265-release-mm1hb-1301926727.tcb.qcloud.la/yanbaocao/index04.mp4?sign=b3dc9c3f13be39ea71057d0cc16ce652&t=1599020808";
          poster = '';
          break;
        case 5:
          src = "https://7265-release-mm1hb-1301926727.tcb.qcloud.la/yanbaocao/index05.mp4?sign=99ed900c9da280941c68bb57826cfb85&t=1599020820";
          poster = '';
          break;
        case 6:
          src = "https://7265-release-mm1hb-1301926727.tcb.qcloud.la/yanbaocao/index06.mp4?sign=59489640478249e43d6741ecd7fae745&t=1599020836";
          poster = '';
          break;
        default:
          break
      }
      videos.push({
        title: (length + i),
        src,
        poster
      });
    }
    return videos;
  }
});